﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web;
using com.dz.ht.common;
using System.Text.RegularExpressions;

namespace com.dz.ht.ui
{
    public partial class FrmRegChapterList : Form
    {
        string m_book_id = string.Empty;
        string m_book_name = string.Empty;
        string m_chapter_list_url = string.Empty;
        string m_encoding =string.Empty;
        string m_reg_chapter = string.Empty;
        public FrmRegChapterList()
        {
            InitializeComponent();
        }

        public FrmRegChapterList(string bookId,string bookName,string url)
        {
            m_book_id = bookId;
            m_book_name = bookName;
            m_chapter_list_url = url;
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSure_Click(object sender, EventArgs e)
        {
            m_encoding=txtSiteEncoding.Text.Trim();
            Encoding t_encoding = Encoding.GetEncoding(m_encoding);
        
            string t_html = NetSiteCatchManager.ReadUrl(m_chapter_list_url, t_encoding);

            DataTable dt = new DataTable();
            DataColumn t_colume_url = new DataColumn("chapterUrl", typeof(string));
          DataColumn t_colume_name = new DataColumn("chapterName", typeof(string));
            dt.Columns.Add(t_colume_url);
            dt.Columns.Add(t_colume_name);

            if (!string.IsNullOrEmpty(t_html))
            {
                m_reg_chapter = txtRegChapterList.Text.Trim();
                MatchCollection t_ma = Regex.Matches(t_html, m_reg_chapter, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                if (t_ma == null || t_ma.Count <= 0)
                {
                    MessageBox.Show("正则错误!");
                    return;
                }
                foreach (Match t_mc in t_ma)
                {
                    DataRow t_row = dt.NewRow();
                    t_row[0] = t_mc.Groups[1].Value.ToString();
                    t_row[1] = t_mc.Groups[2].Value.ToString();
                    dt.Rows.Add(t_row);
                }
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("检查网络!");
            }

        }

        private void 抽取章节内容ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
                return;
            string t_chapter_url = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            Uri t_url = new Uri(m_chapter_list_url);
            string t_site_domain ="http://"+ t_url.Host;
            FrmChpaterContent t_frm = new FrmChpaterContent(m_book_id, m_book_name,t_site_domain,m_encoding,m_chapter_list_url,t_chapter_url,m_reg_chapter);
            t_frm.ShowDialog();
        }
    }
}
